package edu.hawaii.halealohacli.command;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import org.junit.Before;
import org.junit.Test;
import org.wattdepot.client.WattDepotClient;
import edu.hawaii.halealohacli.HaleAlohaCli;

/**
 * Tests MonitorGoal.
 * 
 * @author Jeffrey M. Beck
 * @author Zach Tomaszewski
 * 
 */
public class TestMonitorGoal {

  private MonitorGoal validCmd;

  /**
   * Constructs a valid MonitorGoal command.
   */
  @Before
  public void setUp() {
    WattDepotClient client = new WattDepotClient(HaleAlohaCli.URL);
    String[] args = { "Ilima", "20" };
    SetBaseline.setBaseline(args[0], new double[24]); //all 0s, but set
    validCmd = new MonitorGoal();
    validCmd.initialize(args, client);    
  }
  
  /**
   * Tests that a valid object can be constructed and initialized.
   */
  @Test
  public void testMakeInstance() {
    assertEquals("Command Name", validCmd.getCommandName(), "monitor-goal");
  }
  
  /**
   * Tests that various error messages are produced when this command is
   * initialized with illegal arguments.
   */
  @Test
  public void testIllegalArguments() {
    MonitorGoal cmd = new MonitorGoal();
    WattDepotClient client = new WattDepotClient(HaleAlohaCli.URL);
    String[] validArgs = { "Ilima", "20" };
    
    final String BAD_ARGS = "Invalid arguments"; 

    cmd.initialize(null, client);
    assertTrue("Null args detected", cmd.getResults().contains(BAD_ARGS));
    cmd.initialize(new String[0], client);
    assertTrue("Insufficient args detected", cmd.getResults().contains(BAD_ARGS));    
    cmd.initialize(new String[1], client);
    assertTrue("Insufficient args detected", cmd.getResults().contains(BAD_ARGS));    
    cmd.initialize(new String[4], client);
    assertTrue("Extra args detected", cmd.getResults().contains(BAD_ARGS));    

    cmd.initialize(validArgs, null);
    assertTrue("Bad client detected", cmd.getResults().contains("client"));
    cmd.initialize(new String[2], client);
    assertTrue("Missing source name detected", cmd.getResults().contains("name"));
    cmd.initialize(new String[]{"mySource", "-1"}, client);
    assertTrue("Negative goal detected", cmd.getResults().contains("Goal"));
    cmd.initialize(new String[]{"mySource", "100"}, client);
    assertTrue("Excessive goal detected", cmd.getResults().contains("Goal"));
    cmd.initialize(new String[]{"mySource", "90", "-2"}, client);
    assertTrue("Bad interval detected", cmd.getResults().contains("Interval"));    
  }
  
  /**
   * Tests the MonitorPower's repeated command prints out one line 
   * of two elements each time.  (Since this is just taken from the
   * CurrentPower.Data, the actual values are assumed to be correct if 
   * CurrentPower is correct.) 
   */
  @Test
  public void testOutputLine() {
    String line = validCmd.getResults();
    assertEquals("only one line", 1, line.split("\\n").length);
    String[] tokens = line.split("\\s+"); 
    //System.out.println(java.util.Arrays.toString(tokens));
    assertTrue("six elements per line", tokens.length == 7 && tokens[0].equals(""));
    assertTrue("First token is a time", tokens[1].contains(":"));
    assertTrue("Goal time is <= baseline", 
        Double.parseDouble(tokens[5]) <= Double.parseDouble(tokens[2]));
  }
}